/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "sys/queue.h"
#include "tssp.h"
#include "tssp_table.h"

#ifndef _TSSP_DVB_EIT_H_
#define _TSSP_DVB_EIT_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

struct tssp_dvb_eit_event {
    uint16_t event_id;      /**< event_id */
    uint64_t start_time;    /**< start_time */
    uint32_t duration;      /**< duration */
    uint8_t running_status; /**< running_status */
    uint8_t free_CA_mode;   /**< free_CA_mode */
    struct tssp_descriptors descriptors;

    STAILQ_ENTRY(tssp_dvb_eit_event) entry; /**< Only for internal use, do not access. */
};

STAILQ_HEAD(tssp_dvb_eit_events, tssp_dvb_eit_event);

struct tssp_dvb_eit {
    uint16_t service_id;    /**< service_id */
    uint8_t version;        /**< version_number */
    uint8_t current_next;   /**< current_next_indicator */
    uint16_t ts_id;         /**< transport_stream_id */
    uint16_t network_id;    /**< original_network_id */
    uint8_t segment_last_section_number; /**< segment_last_section_number */
    uint8_t last_table_id;  /**< last_table_id */

    struct tssp_dvb_eit_events events; /**< event loop */

    STAILQ_ENTRY(tssp_dvb_eit) entry; /**< Only for internal use, do not access. */
};

STAILQ_HEAD(tssp_dvb_eit_list, tssp_dvb_eit);

struct tssp_dvb_eit_monitor;

typedef
void (*tssp_dvb_eit_monitor_callback)(struct tssp_dvb_eit_monitor *monitor,
                                      struct tssp_dvb_eit_list *eit_list,
                                      struct tssp_section *section,
                                      void *userdata);

struct tssp_dvb_eit *tssp_dvb_eit_parse(const struct tssp_section *section);
struct tssp_dvb_eit *tssp_dvb_eit_create();
void tssp_dvb_eit_destroy(struct tssp_dvb_eit *eit);
void tssp_dvb_eit_list_destroy(struct tssp_dvb_eit_list *eit_list);
struct tssp_dvb_eit_event *
tssp_dvb_eit_add_event(struct tssp_dvb_eit *eit,
                       uint16_t event_id,
                       uint64_t start_time,
                       uint32_t duration,
                       uint8_t running_status,
                       uint8_t free_CA_mode);

void tssp_dvb_eit_event_destroy(struct tssp_dvb_eit_event *eit_event);

struct tssp_dvb_eit_monitor *
tssp_dvb_eit_monitor_attach(struct tssp *parser,
                            uint16_t service_id,
                            uint8_t table_id,
                            uint8_t current_next,
                            tssp_dvb_eit_monitor_callback callback,
                            void *userdata);
void tssp_dvb_eit_monitor_detach(struct tssp_dvb_eit_monitor *monitor);
void tssp_dvb_eit_monitor_reset(struct tssp_dvb_eit_monitor *monitor);

#ifdef __cplusplus
};
#endif /* __cplusplus */

#endif /* _TSSP_DVB_EIT_H_ */

